package com.jamaskii.dictation;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.jamaskii.ecdict.WordBook;

import java.util.ArrayList;
import java.util.List;

public class ManagerActivity extends BaseActivity {
    public ArrayList<ItemData> itemData=new ArrayList<>();
    public static ManagerActivity instance;
    ItemAdapter itemAdapter;


    public int getStatueBarHeight(){
        int statusBarHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusBarHeight = getResources().getDimensionPixelSize(height);
            return statusBarHeight;
        } catch (Exception e) {
            return 24;
        }
    }

    private void setTopPadding(){
        ConstraintLayout layout=findViewById(R.id.padding) ;
        ViewGroup.LayoutParams layoutParams=layout.getLayoutParams();
        layoutParams.height=getStatueBarHeight();
        layout.setLayoutParams(layoutParams);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_books);
        instance=this;
        init();
    }

    private void init(){
        TextView labelTitleManager=findViewById(R.id.title);
        labelTitleManager.getPaint().setFakeBoldText(true);
        showBooks();
        setBackOnClick();
        setTopPadding();
    }

    private void showBooks(){
        ListView listView=findViewById(R.id.list_books_manager);
        for (WordBook book:HomeActivity.instance.wordBookStream.wordBooks
             ) {
            itemData.add(new ItemData(book,ItemType.MANAGE_BOOK));
        }
        itemAdapter=new ItemAdapter(this,R.layout.item_book_manager,itemData);
        listView.setDivider(getDrawable(R.color.lineGray));
        listView.setDividerHeight(1);
        listView.setAdapter(itemAdapter);
    }

    public void freshLabel(){
        TextView labelDelete=findViewById(R.id.label_delete);
        TextView labelAllSelect=findViewById(R.id.label_all_select);
        int sum=getCheckedItems().size();

        if(sum==HomeActivity.instance.wordBookStream.wordBooks.size()){
            labelAllSelect.setText("全不选");
        }else{
            labelAllSelect.setText("全选");
        }

        if(sum==0){
            labelDelete.setTextColor(getResources().getColor(R.color.defaultTextGary));
            labelDelete.setText("删除");
        }
        else{
            labelDelete.setTextColor(getResources().getColor(R.color.themeColor));
            labelDelete.setText("删除("+sum+")");
        }
    }

    private List<ItemData> getCheckedItems(){
        ArrayList<ItemData> result=new ArrayList<>();
        for (ItemData item:itemData
             ) {
            if(item.checked){
                result.add(item);
            }
        }
        return result;
    }

    public void onLabelClickManager(View v){
        final List<ItemData> checkedItems=getCheckedItems();
        switch (v.getId()){
            case R.id.label_delete:
                int sum=checkedItems.size();
                if(sum==0){
                    return;
                }
                String msg="确定删除这"+checkedItems.size()+"本单词本吗?";
                if(sum==1){
                    msg="确定删除单词本 "+checkedItems.get(0).bookToBeShow.name+" ?";
                }

                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("删除单词本");

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        for (ItemData item:checkedItems
                             ) {
                            if(!HomeActivity.instance.wordBookStream.removeBook(item.bookToBeShow.name)){
                                HomeActivity.instance.showMessage("删除单词本 "+item.bookToBeShow.name+" 出错！\n"+HomeActivity.instance.wordBookStream.error);
                            }
                        }
                        HomeActivity.instance.loadBooks();
                        HomeActivity.instance.linkData();
                        finish();
                    }
                });

                builder.setMessage(msg);
                AlertDialog dialog=builder.create();
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.themeColor));
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.themeColor));
                break;
            case R.id.label_all_select:
                //TextView labelAllSelect=findViewById(R.id.label_all_select);
                boolean selectAll=checkedItems.size()!=HomeActivity.instance.wordBookStream.wordBooks.size();
                for (CheckBox checkBox:itemAdapter.checkBoxes
                ) {
                    checkBox.setChecked(selectAll);
                }
                break;
        }
    }

    public void setBackOnClick(){
        ImageView imageView=findViewById(R.id.image_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}